package org.rslai.autotest.core.page.impl;

import org.openqa.selenium.NoSuchElementException;
import org.rslai.autotest.core.page.AbstractHelper;
import org.rslai.autotest.core.page.AutoIt;

/**
 * AutoIt处理方法实现
 * @author rslai
 */
public class AutoItImpl extends AbstractHelper implements AutoIt {

	@Override
	public void inputUpLoadFileName(String fullFileName) {
		super.sleep(1);
		String winTitle = "";
		// 循环等待 10 秒查找是否出现打开文件对话框
		for (int i=0; i<super.conf.getWebdriverwaitTimeout(); i++) {
			if (this.getAutoIt().winExists("选择文件")) {
				winTitle = "选择文件";
				break;
			} else if (this.getAutoIt().winExists("文件上传")) {
				winTitle = "文件上传";
				break;
			} else if (this.getAutoIt().winExists("选择要上载的文件")) {
				winTitle = "选择要上载的文件";
				break;
			}else if (this.getAutoIt().winExists("打开")) {
				winTitle = "打开";
				break;
			}
			super.sleep(1);
		}

		if (winTitle == "") {
			this.getAutoIt().release();
			throw new NoSuchElementException("未能定位到上传文件对话框，目前只支持以“选择文件”、“文件上传”、“选择要上载的文件”、“打开” 为开头的窗体 title 的上传文件对话框。");
		} else {
			this.getAutoIt().winActivate(winTitle);
			this.getAutoIt().send(fullFileName + "{ENTER}");
			this.getAutoIt().winWaitClose(winTitle);
			super.sleep(1);
			this.getAutoIt().release();
		}

		/* 以上代码由此 AutoIt 脚本文件修改得到
#Region ;**** 参数创建于 ACNWrapper_GUI ****
#PRE_Compile_Both=y
#PRE_Res_requestedExecutionLevel=None
#EndRegion ;**** 参数创建于 ACNWrapper_GUI ****

; 等待10秒得到当前upload的窗体TITLE
Dim $winTitle=""
For $j=1 To 10 Step 1
	For $i=1 To $CmdLine[0]-1 Step 1
		If WinExists($CmdLine[$i]) Then
			$winTitle=$CmdLine[$i];
			ExitLoop(2)
		EndIf
	Next
	Sleep(1000)
Next

; 输入上传文件名称关闭upload窗体
If ($winTitle=="") Then
	Dim $findTitle=""
	For $i=1 To $CmdLine[0]-1 Step 1
		$findTitle = $findTitle & "“" & $CmdLine[$i] & "”"
	Next
	MsgBox(0, "", "按照title（" & $findTitle & "）未找到上传文件窗体！")
Else
	Dim $fileName=$CmdLine[$CmdLine[0]]
	WinActivate($winTitle)
	Send( $fileName & "{Enter}")
	WinWaitClose($winTitle)
	Sleep(1000)
EndIf
		 */
	}

	@Override
	public org.rslai.util.autoit.AutoIt getAutoIt() {
		return org.rslai.util.autoit.AutoIt.getInstance();
	}

}
